package com.wuneng.ircontrol.myUtil.blutetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.wuneng.commonlibrary.util.ToastUtils;

import java.lang.reflect.Method;



public class BluetoothUtil {
    private static BluetoothAdapter btAdapter;
    private static Context context;
    private static Intent cd4btintent;

    public static void init(Context ctx) {
        context = ctx;
        btAdapter = BluetoothAdapter.getDefaultAdapter();
        openBluetooth();
    }

    public static void startDiscovery() {
        ToastUtils.showToast(context, "开始搜索设备");
        btAdapter.cancelDiscovery();
        btAdapter.startDiscovery();
    }

    public static void openBluetooth() {
        if (btAdapter == null) {
            ToastUtils.showToast(context, "本设备不支持蓝牙功能");
            return;
        } else {
            if (!btAdapter.isEnabled()) {
                if (btAdapter.enable()) {
                    //成功开启蓝牙
                    ToastUtils.showToast(context, "成功开启蓝牙");
                    btAdapter.cancelDiscovery();
                    btAdapter.startDiscovery();
                }
            } else {
//                ToastUtils.showToast(context, "开始搜索设备");
//                btAdapter.cancelDiscovery();
//                btAdapter.startDiscovery();
            }
        }
    }

    /**
     * 关闭蓝牙
     *
     * @param context
     */
    public static void closeBluetooth(Context context) {
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        if (btAdapter != null) {
            if (btAdapter.enable()) {
//                Intent intent = new Intent(context, CD4BTService.class);
//                context.stopService(intent);
                btAdapter.disable();
                ToastUtils.showToast(context, "蓝牙已关闭", Toast.LENGTH_LONG);
            }
        }
    }

    public static void connectDevice(BluetoothDevice device) {
        cd4btintent.putExtra("device", device);
        context.startService(cd4btintent);
    }

    public static void pin(BluetoothDevice device) {
        if (device == null) {
            return;
        }

        //配对之前把扫描关闭
        if (btAdapter.isDiscovering()) {
            btAdapter.cancelDiscovery();
        }
        //判断设备是否配对，没有配对在配，配对了就不需要配了
        if (device.getBondState() == BluetoothDevice.BOND_NONE) {
            try {
                Method createBondMethod = device.getClass().getMethod("createBond");
                Boolean returnValue = (Boolean) createBondMethod.invoke(device);
                returnValue.booleanValue();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭蓝牙数据采集
     */
    public static void closeBluetoothData() {
        context.stopService(cd4btintent);
    }


    public static void cancelDiscovery() {
        if (btAdapter.isDiscovering()) {
            btAdapter.cancelDiscovery();
        }

    }

    public static BluetoothAdapter getBTAdapter() {
        return btAdapter;
    }

}
